package com.zy.web.demo.duoxiancheng.demo35;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author <a href="mailto:zhouyong@cqyyt.com">zy</a>
 * @version 1.0
 * @date 2020/10/12 14:11
 */
public class Service35 {

    volatile private static int nextPrintWho = 1;
    private static ReentrantLock lock = new ReentrantLock();
    final  static private Condition conditionA = lock.newCondition();
    final  static private Condition conditionB = lock.newCondition();
    final  static private Condition conditionC = lock.newCondition();

    public static void main(String[] args) {
        Thread A = new Thread(){
            @Override
            public void run() {
                lock.lock();
                try {
                    while ( nextPrintWho != 1 ){
                        conditionA.await();
                    }
                    for (int i = 0; i <3 ; i++) {
                        System.out.println("ThraedA"+(i+1));
                    }
                    nextPrintWho = 2;
                    conditionB.signalAll();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        };
        Thread B = new Thread(){
            @Override
            public void run() {
                lock.lock();
                try {
                    while ( nextPrintWho != 2 ){
                        conditionB.await();
                    }
                    for (int i = 0; i <3 ; i++) {
                        System.out.println("ThraedB"+(i+1));
                    }
                    nextPrintWho = 3;
                    conditionC.signalAll();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        };
        Thread C = new Thread(){
            @Override
            public void run() {
                lock.lock();
                try {
                    while ( nextPrintWho != 3 ){
                        conditionC.await();
                    }
                    for (int i = 0; i <3 ; i++) {
                        System.out.println("ThraedC"+(i+1));
                    }
                    nextPrintWho = 1;
                    conditionA.signalAll();
                }catch (InterruptedException e){
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
            }
        };

        Thread[] threadsA = new Thread[5];
        Thread[] threadsB = new Thread[5];
        Thread[] threadsC = new Thread[5];
        for (int i = 0; i < 5; i++) {
            threadsA[i] = new Thread(A);
            threadsB[i] = new Thread(B);
            threadsC[i] = new Thread(C);
            threadsA[i].start();
            threadsB[i].start();
            threadsC[i].start();
        }
    }

}
